import 'package:dio/dio.dart';
import 'package:flutter_ahj_app/app/HttpRequest.dart';
import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:rxdart/rxdart.dart';

import 'BaseData.dart';

class ChangeDefaultPWDModel extends BaseModel {
  BehaviorSubject<BaseData> _dataSub;

  Stream<BaseData> get dataStream => _dataSub.stream;

  requestMethod(String newPassword) {
    _dataSub = BehaviorSubject();

    HttpRequest.changeDefaultPwd(newPassword).listen((result) {
      BaseData baseData = BaseData.fromJson(result);
      _dataSub.add(baseData);
    }, onError: (e) {
      DioError error = e;
      try {
        BaseData baseData = BaseData.fromJson(error.response.data);
        baseData.code = error.response.data["code"];
        baseData.msg = error.response.data["msg"];
        _dataSub.add(baseData);
      } catch (e) {
        _dataSub.add(null);
        errorData.add(e);
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    if (_dataSub != null) _dataSub.close();
    errorData.close();
  }
}
